﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    /// <summary>
    /// 宠物保险控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InsuranceController : ControllerBase
    {
        private readonly PetDbContext petI;
        public InsuranceController(PetDbContext db)
        {
            petI = db;
        }

        [HttpGet("GetA")]
        public Result Get()
        {
            Result result = new Result();
            result.Data = petI.pet_Insurance.ToList();
            return result;
        }

        [HttpGet("id")]
        public Result GetId(Guid id)
        {
            Result result = new Result();
            result.Data = petI.pet_Insurance.Where(w => w.Id == id).SingleOrDefault();
            return result;
        }


        [HttpPost]
        public Result AddPet([FromBody] Pet_insurance ints)
        {
            Result result = new Result();
            petI.pet_Insurance.Add(ints);
            result.Data = petI.SaveChanges();
            return result;
        }

        [HttpPut]
        public Result Put([FromBody] Pet_insurance ints)
        {
            Result result = new Result();
            petI.pet_Insurance.Update(ints);
            result.Data = petI.SaveChanges();
            return result;
        }

        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_insurance? food = petI.pet_Insurance.Where(w => w.Id == id).SingleOrDefault();
            if (food != null)
                petI.pet_Insurance.Remove(food);
            result.Data = petI.SaveChanges();
            return result;
        }

        [HttpGet("FormHQ")]
        public List<Pet_insurance> Get([FromHeader] string Insured, [FromQuery] string PhoneNumber)
        {
            IQueryable<Pet_insurance> query = petI.pet_Insurance;
            if (!string.IsNullOrEmpty(Insured))
            {
                query.Where(w=>w.Insured==Insured);
            }
            if (!string.IsNullOrEmpty(PhoneNumber))
            {
                query.Where(w => w.PhoneNumber == PhoneNumber);
            }
            return query.ToList();
        }
    }
}
